import java.util.Comparator;
import java.util.Random;
import java.util.PriorityQueue;
public class Main {

    private static double testHeap(Integer[] testData,boolean isHeapify){
        long startTime = System.nanoTime ();
        MaxHeap<Integer> maxHeap;
        if(isHeapify){
            //使用heapify
            maxHeap = new MaxHeap <> (testData);
        }else{
            //不使用heapify
            maxHeap = new MaxHeap <> ();
            for(int num:testData){
                maxHeap.add (num);
            }
        }

        int[] arr = new int[testData.length];
        for ( int i = 0 ; i < testData.length ; i++ ) {
            arr[i] = maxHeap.extractMax ();
        }

        for ( int i = 1 ; i < testData.length; i++ ) {

            if(arr[i - 1] < arr[i])
                throw new IllegalArgumentException ("error");
        }

        System.out.println ("Test complex");
        long endTime = System.nanoTime ();
        return (endTime - startTime) / 100000000.0;
    }

    private class Freq{
        public int e ,freq;
        public Freq(int e,int freq){
            this.e = e;
            this.freq = freq;
        }
    }

    public static void main(String[] args) {
	// write your code here

//        int n = 1000000;
//        MaxHeap<Integer> maxHeap = new MaxHeap <> ();
//        Random random = new Random ();
//        for ( int i = 0 ; i < n ; i++ ) {
//            maxHeap.add (random.nextInt (Integer.MAX_VALUE));
//        }
//
//        int[] arr = new int[n];
//        for ( int i = 0 ; i < n ; i++ ) {
//            arr[i] = maxHeap.extractMax ();
//        }
//
//        for ( int i = 1 ; i < n; i++ ) {
//
//            if(arr[i - 1] < arr[i])
//                throw new IllegalArgumentException ("error");
//        }
//        System.out.println ("Test complex");
        int n = 100000;
        Random random = new Random ();
        Integer[] testData = new Integer[n];
        for ( int i = 0 ; i < n  ; i++ ) {
            testData[i] = random.nextInt (Integer.MAX_VALUE);
        }

        double time1 = testHeap (testData,false);
        System.out.println ("without heapify,the time is " + time1 +"s");
        double time2= testHeap (testData,true);
        System.out.println ("without heapify,the time is " + time2 +"s");


        PriorityQueue<Freq> pq = new PriorityQueue <> (new Comparator <Freq> (){
            @Override
            public int compare(Freq o1 , Freq o2) {
                return o1.freq - o2.freq;
            }
        });

    }
}
